<script setup>
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls'
import {ref,onMounted} from 'vue'
let firstThree = ref()
const scene = new THREE.Scene()

const geometry = new THREE.BufferGeometry(100,100,100)
// 顶点坐标
const vertices = new Float32Array([
  0, 0, 0, //顶点1坐标
  50, 0, 0, //顶点2坐标
  0, 100, 0, //顶点3坐标
  0, 0, 10, //顶点4坐标
  0, 0, 100, //顶点5坐标
  50, 0, 10, //顶点6坐标
])
// 此时我们不能直接使用这个数组,需要用THREE的API转化下
// BufferAttribute两个参数 ，顶点数据-几个元素为一组
const beffer = new THREE.BufferAttribute(vertices,3)
// 类型化数组顶点定义
const indexes = new THREE.Uint16Array([0,1,2,0,2,3])
// 几何体顶点索引的定义
geometry.index = new THREE.BufferAttribute(indexes,1)
// 设置顶点位置属性
geometry.attributes.position = beffer;
// 线模型所使用的点材质
const metaerial = new THREE.LineBasicMaterial({
  color:'#a00',
  shininess:100,  // 高光部分的亮度
  specular:0x444444,  // 高光部分的颜色
})

// 闭合线条
const line = new THREE.LineLoop(geometry, metaerial); 
line.position.set(0,0,0)
scene.add(line)


// 环境光
const ambient = new THREE.AmbientLight("#fff",0.8)
scene.add(ambient)

const width = window.innerWidth;
const height = window.innerHeight;

const camera = new THREE.PerspectiveCamera(100,width/height,1,8000)
camera.position.set(100,100,200)
camera.lookAt(0,0,0) 

// 解决渲染出来的图像有锯齿
const renderer = new THREE.WebGLRenderer({
  antialias:true, // 启动抗锯齿
});
renderer.setSize(width,height)
// 设置屏幕像素比
renderer.setPixelRatio(window.devicePixelRatio)
renderer.render(scene,camera)

const controls = new OrbitControls(camera,renderer.domElement)
controls.addEventListener("change",function(){
  // 重新渲染，必须有这步操作，否则数据改变，但无法看到
  renderer.render(scene,camera)
})
onMounted(()=>{
  document.getElementById("firstThree").appendChild(renderer.domElement)
})

</script>

<template>
  <div id="firstThree" ref="firstThree"></div>
</template>

<style scoped>
  #firstThree{
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: absolute;
  }
</style>
